#include <bits/stdc++.h>
// 2025/04/05
// tag:
// Author: Forever
using namespace std;
using int64 = int64_t;

class UF {
public:
    int parents[10001], cnt;

    UF(int n)
    {
        cnt = n;
        for (int i = 1; i <= n; i++)
            parents[i] = i;
    };

    int find(int i)
    {
        if (parents[i] != i)
            return parents[i] = find(parents[i]);
        return parents[i];
    }

    void un(int i, int j)
    {
        int ri = find(i), rj = find(j);
        if (ri == rj)
            return;
        parents[ri] = rj;
        cnt--;
    }

    int count()
    {
        return cnt;
    }

    bool connected(int p, int q)
    {
        return find(p) == find(q);
    }
};

class edge {
public:
    int u, v, w1, w2, id;
    edge(int u, int v, int w1, int w2, int id)
        : u(u)
        , v(v)
        , w1(w1)
        , w2(w2)
        , id(id) { };
    edge()
        : u(0)
        , v(0)
        , w1(0)
        , w2(0) 
        , id(0) { };
};

bool cmp1(edge& e1, edge& e2){
    return e1.w1 < e2.w1;
}

bool cmp2(edge& e1, edge& e2){
    return e1.w2 < e2.w2;
}

int n, m, k, ans, ct;
edge edges[20001];
pair<int, int> ans2[10001];

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k >> m;
    m--;
    for (int i = 1; i <= m; i++) {
        int u, v, w1, w2;
        cin >> u >> v >> w1 >> w2;
        edges[i] = edge(u, v, w1, w2, i);
    }
    sort(edges + 1, edges + m + 1, cmp1);
    UF uf(n);
    for (int i = 1, cnt = 0; i <= m, cnt != k; i++) {
        if (uf.connected(edges[i].u, edges[i].v))
            continue;
        uf.un(edges[i].u, edges[i].v);
        ans = max(edges[i].w1, ans);
        cnt++;
        ans2[++ct] = {edges[i].id, 1};
    }
    sort(edges + 1, edges + m + 1, cmp2);
    for (int i = 1; i <= m; i++) {
        if (uf.connected(edges[i].u, edges[i].v))
            continue;
        uf.un(edges[i].u, edges[i].v);
        ans = max(edges[i].w2, ans);
        ans2[++ct] = {edges[i].id, 2};
    }
    sort(ans2 + 1, ans2 + ct + 1);
    cout << ans << endl;
    for (int i = 1; i <= ct; i++) {
        cout << ans2[i].first << " " << ans2[i].second << endl;
    }
    return 0;
}